<template>
	<view class="w-picker-view">
		<picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange" @pickstart="pickstart" @pickend="pickend">
			<picker-view-column>
				<view class="w-picker-item" v-for="(item,index) in range" :key="index">{{item[nodeKey]}}</view>
			</picker-view-column>
		</picker-view>
	</view>
</template>

<script>
	export default {
		props:{
			itemHeight:{
				type:String,
				default:"44px"
			},
			options:{
				type:[Array,Object],
				default(){
					return []
				}
			},
			value:{
				type:String,
				default:""
			},
			defaultType:{
				type:String,
				default:"label"
			},
			defaultProps:{
				type:Object,
				default(){
					return{
						label:"label",
						value:"value"
					}
				}
			}
		},
		data() {
			return {
				pickVal:[],
				thisDefaultProps:this.defaultProps
			};
		},
		created() {
			this.initData(true);
		},
		computed:{
			nodeKey(){
				return this.thisDefaultProps.label;
			},
			nodeValue(){
				return this.thisDefaultProps.value;
			},
			range(){				
				return this.options
			}
		},
		watch:{
			value(val){
				if(this.options.length!=0){
					this.initData(true);
				}
			},
			options(val){
				this.initData(true);
			}
		},
		onReady() {
			if(this.options&&this.options.length!=0){
				this.initData(true);
			}
		},
		methods:{
			pickstart(){
				this.$emit("changeConfirmFlag", false)
			},
			pickend(){
				this.$emit("changeConfirmFlag", true)
			},
			updateValue(value){
				this.initData(true,value)
			},
			initData(flag,upValue){
				let dVal=this.value||"";
				if(upValue!=undefined){
					dVal=upValue
				}
				let data=this.options;
				if(!data){
					data=[]
				}
				let pickVal=[0];
				let cur=null;
				let label="";
				let value,idx;
				if(this.defaultType==this.nodeValue){
					value=data.find((v)=>v[this.nodeValue]==dVal);
					idx=data.findIndex((v)=>v[this.nodeValue]==dVal);
				}else{
					value=data.find((v)=>v[this.nodeKey]==dVal);
					idx=data.findIndex((v)=>v[this.nodeKey]==dVal);
				}
				pickVal=[idx!=-1?idx:0];
				this.$nextTick(()=>{
					this.pickVal=pickVal;
				});
				if(this.defaultType==this.nodeValue){
					this.$emit("change",{
						result:value?value[this.nodeKey]:(data&&data.length>0?data[0][this.nodeKey]:null),
						value:dVal||(data&&data.length>0?data[0][this.nodeValue]:null),
						obj:value?value:(data&&data.length>0?data[0]:null),
						flag:flag
					})
				}else{
					this.$emit("change",{
						result:dVal||(data&&data.length>0?data[0][this.nodeKey]:null),
						value:value?value[this.nodeValue]:(data&&data.length>0?data[0][this.nodeValue]:null),
						obj:value?value:(data&&data.length>0?data[0]:null),
						flag:flag
					})
				}
				
			},
			handlerChange(e){
				let arr=[...e.detail.value];
				let pickVal=[arr[0]||0];
				let data=this.options;
				let cur=data[arr[0]];
				let label="";
				let value="";
				this.$nextTick(()=>{
					this.pickVal=pickVal;
				});
				this.$emit("change",{
					result:cur[this.nodeKey],
					value:cur[this.nodeValue],
					obj:cur
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "../w-picker/w-picker.css";
</style>
